package cn.com.dhcc.ddi.usermgr.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.service.CmUserMgrService;
import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.vo.UserInfoVo;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.YESNO;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.ddi.node.service.NodeInfoService;
import cn.com.dhcc.ddi.node.vo.NodeInfo;
import cn.com.dhcc.ddi.usermgr.service.UserInfoService;

/**
 * 
    * @ClassName: UserMgrController
    * @Description: 用户管理
    * @author dxy
    * @date 2017-3-28
    *
 */
@Controller
@RequestMapping("/usermgr")
public class UserMgrController extends BaseController{
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private NodeInfoService nodeInfoService;
	
	@Autowired
	private CmUserMgrService userMgrService;

	
	/**
	 * 
	    * @Title: userNodeMgr
	    * @Description: 配置用户节点管理权限
	    * @param @param id
	    * @param @param model
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping(value = "user_node_mgr")
	public String userNodeMgr(String id, Model model) {
		try {
			List<NodeInfo> allNodeInfoList=nodeInfoService.getOrgCode();

			List<NodeInfo> typeOrgInfoList= nodeInfoService.getNodeOrgCode();
			/*for(NodeInfo de :allNodeInfoList){
				if(de.getOrgcode().endsWith("0000")){					
					typeOrgInfoList.add(de);
				}
			}*/
			model.addAttribute("nodeList", allNodeInfoList);
			model.addAttribute("orgTypeList", typeOrgInfoList);
			UserInfoVo vo = userInfoService.get(id);
			model.addAttribute(VO_KEY, vo);
		} catch (ServiceException e) {
			return toAlertPage(model, e.getMessage());
		}
		return "pub/sysmgr/usermgr/user_node_mgr";
	}
	
	@RequestMapping(value="node_mgr_user")
	@ResponseBody
	public Map<String, Object> updateUser(Model model,UserInfoVo user,HttpSession session){
		try{
			user.setModified(new Date());
			user.setAdmin(YESNO.NO.getStatus());
			if(StrUtil.isBlank(user.getExt2())){
				user.setExt2("");
			}
			userMgrService.updateUser(user);
		} catch (NiceException e) {
			return jsonFailResult(e.getMessage());
		} catch (ServiceException e) {
			return jsonFailResult("配置失败");
		}
		return jsonSuccessResult("配置成功");
	}

}
